package com.ruoyi.web.controller.dormitory;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.dormitory.domain.TbDormitory;
import com.ruoyi.dormitory.service.ITbDormitoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 宿舍Controller
 *
 * @author ruoyi
 * @date 2023-12-28
 */
@RestController
@RequestMapping("/dormitory/dormitory")
public class TbDormitoryController extends BaseController
{
    @Autowired
    private ITbDormitoryService tbDormitoryService;

    /**
     * 查询宿舍列表
     */
    @PreAuthorize("@ss.hasPermi('dormitory:dormitory:list')")
    @GetMapping("/list")
    public TableDataInfo list(TbDormitory tbDormitory)
    {
        startPage();
        List<TbDormitory> list = tbDormitoryService.selectTbDormitoryList(tbDormitory);
        return getDataTable(list);
    }

    /**
     * 导出宿舍列表
     */
    @PreAuthorize("@ss.hasPermi('dormitory:dormitory:export')")
    @Log(title = "宿舍", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TbDormitory tbDormitory)
    {
        List<TbDormitory> list = tbDormitoryService.selectTbDormitoryList(tbDormitory);
        ExcelUtil<TbDormitory> util = new ExcelUtil<TbDormitory>(TbDormitory.class);
        util.exportExcel(response, list, "宿舍数据");
    }

    /**
     * 获取宿舍详细信息
     */
    @PreAuthorize("@ss.hasPermi('dormitory:dormitory:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id)
    {
        return success(tbDormitoryService.selectTbDormitoryById(id));
    }

    /**
     * 新增宿舍
     */
    @PreAuthorize("@ss.hasPermi('dormitory:dormitory:add')")
    @Log(title = "宿舍", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TbDormitory tbDormitory)
    {
        return toAjax(tbDormitoryService.insertTbDormitory(tbDormitory));
    }

    /**
     * 修改宿舍
     */
    @PreAuthorize("@ss.hasPermi('dormitory:dormitory:edit')")
    @Log(title = "宿舍", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TbDormitory tbDormitory)
    {
        return toAjax(tbDormitoryService.updateTbDormitory(tbDormitory));
    }

    /**
     * 删除宿舍
     */
    @PreAuthorize("@ss.hasPermi('dormitory:dormitory:remove')")
    @Log(title = "宿舍", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids)
    {
        return toAjax(tbDormitoryService.deleteTbDormitoryByIds(ids));
    }
}
